// pages/my/my.js
var api = require("../../utils/api.js");
var util = require("../../utils/util.js");
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    userPhone: null,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    coin: 0,
    day: 0,
    btns: [{
        id: 1,
        img: "../../res/my/icon-link00.png",
        title: "每日签到",
      },
      {
        id: 2,
        img: "../../res/my/icon-link01.png",
        title: "邀请好友",
      },
      {
        id: 3,
        img: "../../res/my/icon-link02.png",
        title: "我的订单",
      },
      {
        id: 4,
        img: "../../res/my/icon-link03.png",
        title: "收货地址",
      }, {
        id: 5,
        img: "../../res/my/icon-link04.png",
        title: "排行榜",
      },
      {
        id: 6,
        img: "../../res/my/icon-link05.png",
        title: "我的抽奖",
      },
      {
        id: 7,
        img: "../../res/my/icon-link06.png",
        title: "互助记录",
      },
      {
        id: 8,
        img: "../../res/my/icon-link07.png",
        title: "规则说明",
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      this.getUserInfo();
    }
  },
  //获取用户信息
  getUserInfo: function() {
    wx.getUserInfo({
      success: res => {
        api.updateUserInfo(app.globalData.userStatus.uid, res.userInfo.nickName, res.userInfo.avatarUrl);
        app.globalData.userInfo = res.userInfo
        this.setData({
          userInfo: res.userInfo,
        })
      }
    })
  },
  //绑定手机号
  getPhoneNumber: function(e) {
    var info = e.detail;
    var app = getApp();
    api.getUserPhone(app.globalData.userStatus.uid, app.globalData.userStatus.session, info.iv, info.encryptedData, res => {
      this.setData({
        userPhone: res,
      });
    });
  },
  //功能按钮
  clickFunction: function(e) {
    switch (e.currentTarget.dataset.item.id) {
      case 1:
        util.toSign(1);
        break;
      case 2:
        break;
      case 3:
        util.toMyOrder();
        break;
      case 4:
        wx.chooseAddress();
        break;
      case 5:
        util.toRank();
        break;
      case 6:
        util.toMyLuck();
        break;
      case 7:
        util.toHelpRecord();
        break;
      case 8:
        util.toAboutUsRule();
        break;
      default:
        break;
    }
  },

  clickSetting: function(e) {
    wx.openSetting();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (app.globalData.userStatus) {
      this.setData({
        userPhone: app.globalData.userStatus.phone,
        coin: parseFloat(app.globalData.userStatus.point),
        day: app.globalData.userStatus.created_at,
      });
    } else {
      app.userStatusReadyCallback = res => {
        this.setData({
          userPhone: res.phone,
          coin: parseFloat(res.userStatus.point),
          day: res.userStatus.created_at,
        });
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return app.shareSeekHelp();
  }
})